package cn.anecansaitin.firecrafting.common.util;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import net.minecraft.core.NonNullList;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.material.Fluid;
import net.minecraftforge.common.crafting.CraftingHelper;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.registries.ForgeRegistries;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

//json读取工具类
public class ModJsonReader {
    //读取物品集合
    public static List<ItemStack> itemsFromJson(JsonObject json, String memberName) {
        List<ItemStack> list = new ArrayList<>();
        JsonArray items = json.getAsJsonArray(memberName);
        items.forEach((jsonElement -> {
            ItemStack itemStack = CraftingHelper.getItemStack(jsonElement.getAsJsonObject(), true, true);
            list.add(itemStack);
        }));
        return list;
    }

    //读取成分集合
    public static NonNullList<Ingredient> ingredientsFromJson(JsonArray pIngredientArray) {
        NonNullList<Ingredient> nonnulllist = NonNullList.create();

        for (int i = 0; i < pIngredientArray.size(); ++i) {
            Ingredient ingredient = Ingredient.fromJson(pIngredientArray.get(i));
            if (net.minecraftforge.common.ForgeConfig.SERVER.skipEmptyShapelessCheck.get() || !ingredient.isEmpty()) {
                nonnulllist.add(ingredient);
            }
        }

        return nonnulllist;
    }

    //读取方块集合
    public static List<Block> blocksFromJson(JsonObject json, String memberName) {
        List<Block> list = new ArrayList<>();
        json.getAsJsonArray(memberName).forEach((j) -> list.add(ForgeRegistries.BLOCKS.getValue(new ResourceLocation(j.getAsJsonObject().get("block").getAsString()))));

        return list;
    }

    //读取流体
    public static FluidStack fluidFromJson(JsonObject json, String memberName) {
        JsonObject object = json.get(memberName).getAsJsonObject();
        String fluid = object.get("fluid").getAsString();
        int amount = object.get("amount").getAsInt();
        Fluid value = ForgeRegistries.FLUIDS.getValue(new ResourceLocation(fluid));
        return new FluidStack(Objects.requireNonNull(value), amount);
    }

    public static List<EntityType<?>> entityTypesFromJson(JsonObject json, String memberName) {
        ArrayList<EntityType<?>> list = new ArrayList<>();
        json.getAsJsonArray(memberName).forEach((j) -> list.add(ForgeRegistries.ENTITIES.getValue(new ResourceLocation(j.getAsJsonObject().get("entity").getAsString()))));
        return list;
    }

    public static List<MobEffectInstance> effectFromJson(JsonObject json, String memberName) {
        ArrayList<MobEffectInstance> list = new ArrayList<>();
        json.getAsJsonArray(memberName).forEach((j) -> {
            JsonObject j1 = j.getAsJsonObject();
            MobEffect effect = Objects.requireNonNull(ForgeRegistries.MOB_EFFECTS.getValue(new ResourceLocation(j1.get("effect").getAsString())));
            int amplifier = j1.get("amplifier").getAsInt();
            int duration = j1.get("duration").getAsInt();
            boolean icon = j1.get("icon").getAsBoolean();
            boolean particle = j1.get("particle").getAsBoolean();
            boolean ambient = j1.get("ambient").getAsBoolean();
            list.add(new MobEffectInstance(effect, duration, amplifier, ambient, particle, icon));
        });
        return list;
    }
}
